package dao;

import core.TestData;
import core.Comment;
import core.User;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.ejb.Stateless;


/**
 *
 * @author Michal Horak
 */
@Stateless
public class CommentDAO implements IComment{

    @Override
    public void createComment(Comment comment) {
        if (comment == null){
            throw new RuntimeException("null paprametr");
        } else {
            Integer commentId = this.getMaxCommentId();
            comment.setId(commentId);
            TestData.COMM_TABLE.put(commentId, comment);
        }
    }

    @Override
    public void updateComment(Comment comment) {
         if(comment == null || !TestData.COMM_TABLE.containsKey(comment.getId()))
        {
            throw new RuntimeException("Komentář nelze vytvořit" );
        }
        TestData.COMM_TABLE.put(comment.getId(), comment);
    }

    @Override
    public void deleteComment(Comment comment) {
         if(comment == null)
        {
            throw new RuntimeException("null");
        }
        TestData.COMM_TABLE.remove(comment.getId());
    }

    @Override
    public Comment findComment(int commentId) {
        return TestData.COMM_TABLE.get(commentId);
    }

    @Override
    public List<Comment> findAllComments() {

       return new ArrayList<Comment>(TestData.COMM_TABLE.values());
    }

    @Override
    public Comment findComment(String commentname) {
        Comment a = null;
        
        for (Map.Entry<Integer, Comment> entry : TestData.COMM_TABLE.entrySet()){
            a = entry.getValue();
            if (a.getTitle().equals(commentname)){
                return a;
            }
        }
        return a;
        
    }
    
    protected Integer getMaxCommentId()
    {
       
        return TestData.COMM_TABLE.size()+1;
    }

    @Override
    public List<Comment> findAllCommentsFromUser(User id) {
        List<Comment> a = new ArrayList<Comment>();
        
        for (Map.Entry<Integer, Comment> entry : TestData.COMM_TABLE.entrySet()){
            Comment c = entry.getValue();
            
            if (c.getAutorId() == id.getId()){
                a.add(c);
            }
            
        }
        return a;
    }

    
}
